package jp.co.dailyReport.staff.sql;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.Utilities;
import jp.co.dailyReport.staff.Staff;

/**
 * 社員情報を取得するクラスです。
 */
public class StaffsLoadExecuter implements Executer {

	private static Logger log = Logger.getLogger(StaffsLoadExecuter.class);

	/**
	 * 社員情報を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return 社員オブジェクトの配列
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "select * from staff where delFlg=0 order by staffId";
		log.debug("sql: " + sql);
		Vector answer = new Vector();
		try {
			pstmt = conn.prepareStatement(sql);
			Staff staff;
			for(rset = pstmt.executeQuery(); rset.next(); answer.add(staff)) {
				staff = new Staff();
				staff.setStaffId(rset.getInt("staffId"));
				staff.setName(Utilities.convUniqueCharacter(rset.getString("name")));
				staff.setUsr(Utilities.convUniqueCharacter(rset.getString("usr")));
				staff.setKeyCode(Utilities.convUniqueCharacter(rset.getString("passwd")));
				staff.setEmploymentSystem(rset.getInt("employmentForm"));
				staff.setPosition(rset.getInt("authority"));
				staff.setTargetUnitPrice(rset.getInt("targetUnitPrice"));
				staff.setDelFlg(Utilities.decimalToBoolean(rset.getInt("delFlg")));
				staff.setUpDay(Utilities.getCalendar(rset.getDate("upDay")));
			}
		} finally {
			if(rset != null) 
				rset.close();
			if(pstmt != null) 
				pstmt.close();
		}
		return (Staff[])answer.toArray(new Staff[0]);
	}
}